package com.salim.leetcode.$10;

import lombok.extern.slf4j.Slf4j;

/**
 * Created with IntelliJ IDEA.
 * User: clevo
 * Date: 15-9-17
 * Time: 上午11:31
 * To change this template use File | Settings | File Templates.
 */
@Slf4j
public class RegularExpressionMatching {

    public boolean isMatch(String s, String p) {
        if (p.equals("")) {
            if(s.equals("")) {
                return true;
            }else{
                return false;
            }
        }
        boolean first = false;
        if(s.length()>=1){
            if(s.charAt(0)==p.charAt(0)||p.charAt(0)=='.'){
                first = true;
            }
        }

        if(p.length()>=2 && p.charAt(1)=='*'){
            return (first && isMatch(s.substring(1),p)) || isMatch(s,p.substring(2));
        }else{
            return first && isMatch(s.substring(1),p.substring(1));
        }
    }

    public static void main(String[] args){
        RegularExpressionMatching r = new RegularExpressionMatching();
        log.info("{}",r.isMatch("aa","a"));
        log.info("{}",r.isMatch("aa","a*"));
        log.info("{}",r.isMatch("ab",".*"));
        log.info("{}",r.isMatch("aab","c*a*b"));
        log.info("{}",r.isMatch("mississippi","mis*is*p*."));
        log.info("{}",r.isMatch("abcd","d*"));
        log.info("{}",r.isMatch("aaa","aaaa"));
        log.info("{}",r.isMatch("ab",".*c"));
        log.info("{}",r.isMatch("a","ab*"));
        log.info("{}",r.isMatch( "aaa", "a*a"));
    }
}

/**
 * 动态规划解法
 */
enum Result {
    TRUE, FALSE
}

/**
 * 从上至下
 */
class Solution2 {
    Result[][] memo;

    public boolean isMatch(String text, String pattern) {
        memo = new Result[text.length() + 1][pattern.length() + 1];
        return dp(0, 0, text, pattern);
    }

    public boolean dp(int i, int j, String text, String pattern) {
        if (memo[i][j] != null) {
            return memo[i][j] == Result.TRUE;
        }
        boolean ans;
        if (j == pattern.length()){
            ans = i == text.length();
        } else{
            boolean first_match = (i < text.length() &&
                    (pattern.charAt(j) == text.charAt(i) ||
                            pattern.charAt(j) == '.'));

            if (j + 1 < pattern.length() && pattern.charAt(j+1) == '*'){
                ans = (dp(i, j+2, text, pattern) ||
                        first_match && dp(i+1, j, text, pattern));
            } else {
                ans = first_match && dp(i+1, j+1, text, pattern);
            }
        }
        memo[i][j] = ans ? Result.TRUE : Result.FALSE;
        return ans;
    }
}

/**
 * 从下至上
 */
class Solution3 {
    public boolean isMatch(String text, String pattern) {
        boolean[][] dp = new boolean[text.length() + 1][pattern.length() + 1];
        dp[text.length()][pattern.length()] = true;

        for (int i = text.length(); i >= 0; i--){
            for (int j = pattern.length() - 1; j >= 0; j--){
                boolean first_match = (i < text.length() &&
                        (pattern.charAt(j) == text.charAt(i) ||
                                pattern.charAt(j) == '.'));
                if (j + 1 < pattern.length() && pattern.charAt(j+1) == '*'){
                    dp[i][j] = dp[i][j+2] || first_match && dp[i+1][j];
                } else {
                    dp[i][j] = first_match && dp[i+1][j+1];
                }
            }
        }
        return dp[0][0];
    }
}
